var express = require('express');
var router = express.Router();
const should = require('should');
const path = require('path');
const {
    Configuration,
    Web3jService,
    CompileService
} = require('nodejs-sdk/packages/api');

const config = new Configuration('./conf/config.json'); //user
const contractPath = './contracts/v4/supplychain.sol';
const compileService = new CompileService(config);
const web3jService = new Web3jService(config);
const contractClass = compileService.compile(contractPath);
const Smart = contractClass.newInstance();
const supplychain_address = require('../contracts/conf.js').supplychainAddress

/* GET home page. */
router.get('/', async function (req,res) {
	console.log("supply chain : " + supplychain_address);
    let supplychain = contractClass.newInstance();
    supplychain.$load(web3jService, supplychain_address);
	try{
		const wheel = await supplychain.register("GoWheel",100,'0x7b4426215c0d9bfac55f5c46648a16a7ce38ab71',0);
		const car = await supplychain.register("Benz",500,'0x64e8110e0582a0018985a812afcfb1223d8cd05d',0)
		const bank = await supplychain.register("SSBank",1000,'0x77610c6fa001bb97bf71ad335698b36545bc8caf',1)
		res.json({
			errno:0,
			data:{
				ret:"Regist 3 accounts Success"
			}
		})
	}catch(exc){
		res.json({
			errno:1,
			data:{
				ret:"Regist fail"
			}
		})
	}
});

module.exports = router;
